﻿/*
 * Created by SharpDevelop.
 * User: x0105974
 * Date: 6/7/2010
 * Time: 4:50 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data;
using stdf;
using System.Text;
using System.IO;

namespace stdf2csv
{
	class Program
	{
        public static string strToCsvString(string instr)
        {
            string outstr = instr;
            if (instr.Contains(",") || instr.Contains(" "))
                outstr = '"' + instr + '"';

            return outstr;
        }
		
		public static void dataTableToCSV( ref DataTable table, ref DataTable headerTable, string csvFileName) {
			string sep = ",";
			bool convertHeaderTable = true;
			
			StringBuilder sb = new StringBuilder();
			//
			// Convert Header Table
			//
			if(convertHeaderTable) {
				foreach (DataColumn col in headerTable.Columns) {
                    sb.Append( strToCsvString(col.ColumnName) + sep);
				}
				sb.Append( Environment.NewLine);
				
				foreach (DataRow row in headerTable.Rows)
	            {
	                for (int i = 0; i < headerTable.Columns.Count; i++)
	                {
	                    sb.Append(row[i].ToString() + sep);
	                }
	
	                sb.Append(Environment.NewLine);
	            }
			}			
			//
			// Convert Data Table
			//
			{
				sb.Append( "USN" + sep);
				foreach (DataColumn col in table.Columns) {
                    sb.Append( strToCsvString(col.ColumnName) + sep);
				}
				sb.Append( Environment.NewLine);
				
				int rowCount = 0;
				foreach (DataRow row in table.Rows)
	            {
					rowCount++;
					sb.Append( rowCount.ToString() + sep);
	                for (int i = 0; i < table.Columns.Count; i++)
	                {
	                    sb.Append(row[i].ToString() + sep);
	                }
	
	                sb.Append(Environment.NewLine);
	            }
			}

            StreamWriter writer = new StreamWriter(csvFileName);
			writer.Write( sb.ToString());
            writer.Close();
		}
		public static void Main(string[] args)
		{
			Console.WriteLine("STDF2CSV");
			
			// TODO: Implement Functionality Here
			
			foreach( string arg in args) {
			
				string fileName = arg;
				Console.WriteLine( "Processing file " + fileName);
				
				string fileExt  = System.IO.Path.GetExtension(fileName);

                string csvFileName = fileName;
                csvFileName = csvFileName.Replace(fileExt, ".csv");

				stdf.StdfFile stdfFile = new stdf.StdfFile();
                        
                        stdfFile.loadMIR = true;
                        stdfFile.loadSDR = true;
                        stdfFile.loadWIR = true;
                        stdfFile.loadPRR = true;
                        //stdfFile.loadPRR = true;
                        stdfFile.loadPTR = true;
                        stdfFile.loadFTR = true;
                        stdfFile.ignoreAlarm = true;

                        bool success = stdfFile.open(fileName, stdf.RWMode.ReadOnly);
                        if (success)
                        {
                        	DataTable table = new DataTable();
							DataTable headerTable = new DataTable();
                            success = stdfFile.read(ref table, ref headerTable);
                            dataTableToCSV( ref table, ref headerTable, csvFileName);
                        }
			
			}

		}
		

	}
}